﻿//======================================================================
//
//        Copyright (C) 苏州逸仙软件    
//        All rights reserved
//
//        filename : D_MenuExp.cs
//        description :
//
//        created by 周济 at 2010/8/22 16:09:43
//        mail:13912699233@139.com
//
//======================================================================


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace ksyx.User.Dal
{
    public class D_MenuExp : D_Menu
    {

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool ExistsExp(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Menu");
            strSql.Append(" where id=@id and flag=1 ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            return ksyx.DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int AddExp(ksyx.User.Model.T_Menu model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Menu(");
            strSql.Append("MenuName,MenuURL,MenuParentID,MenuLevel,MenuParentList,MenuSonList,Flag)");
            strSql.Append(" values (");
            strSql.Append("@MenuName,@MenuURL,@MenuParentID,@MenuLevel,@MenuParentList,@MenuSonList,1)");
            strSql.Append(";update T_Menu set MenuParentList=dbo.f_MenuPList(id),MenuSonList=dbo.f_MenuSList(id) where id=@@IDENTITY;select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@MenuName", SqlDbType.NVarChar,50),
					new SqlParameter("@MenuURL", SqlDbType.NVarChar,200),
					new SqlParameter("@MenuParentID", SqlDbType.Int,4),
					new SqlParameter("@MenuLevel", SqlDbType.Int,4),
					new SqlParameter("@MenuParentList", SqlDbType.NVarChar,200),
					new SqlParameter("@MenuSonList", SqlDbType.NVarChar,200),
					new SqlParameter("@Flag", SqlDbType.Bit,1)};
            parameters[0].Value = model.MenuName;
            parameters[1].Value = model.MenuURL;
            parameters[2].Value = model.MenuParentID;
            parameters[3].Value = model.MenuLevel;
            parameters[4].Value = model.MenuParentList;
            parameters[5].Value = model.MenuSonList;
            parameters[6].Value = model.Flag;

            object obj = ksyx.DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int UpdateExp(ksyx.User.Model.T_Menu model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Menu set ");
            strSql.Append("MenuName=@MenuName,");
            strSql.Append("MenuURL=@MenuURL,");
            strSql.Append("MenuParentID=@MenuParentID,");
            strSql.Append("MenuLevel=@MenuLevel,");
            strSql.Append("MenuParentList=dbo.f_MenuPList(id),");
            strSql.Append("MenuSonList=dbo.f_MenuSList(id),");
            strSql.Append("Flag=Flag");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@MenuName", SqlDbType.NVarChar,50),
					new SqlParameter("@MenuURL", SqlDbType.NVarChar,200),
					new SqlParameter("@MenuParentID", SqlDbType.Int,4),
					new SqlParameter("@MenuLevel", SqlDbType.Int,4),
					new SqlParameter("@MenuParentList", SqlDbType.NVarChar,200),
					new SqlParameter("@MenuSonList", SqlDbType.NVarChar,200),
					new SqlParameter("@Flag", SqlDbType.Bit,1)};
            parameters[0].Value = model.id;
            parameters[1].Value = model.MenuName;
            parameters[2].Value = model.MenuURL;
            parameters[3].Value = model.MenuParentID;
            parameters[4].Value = model.MenuLevel;
            parameters[5].Value = model.MenuParentList;
            parameters[6].Value = model.MenuSonList;
            parameters[7].Value = model.Flag;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int DelExp(int id)
        {
            return " T_Menu ".Exp_UpdateFlag(id, YXSQL.SQLConType.C1);

        }
        /// <summary>
        /// 判断菜单url是否重复
        /// </summary>
        /// <param name="strURL"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool IsMenuURL(string strURL, int id)
        {
            return " T_Menu ".Exp_ExistName(id, " MenuURL ", strURL, SqlDbType.NVarChar, "");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<Model.T_MenuExp> GetMenuList()
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append(" SELECT     id, MenuName, MenuURL, MenuParentID, MenuLevel, MenuParentList, MenuSonList, Flag, PName ");
            strSql.Append(" FROM         (SELECT     a.id, a.MenuName, a.MenuURL, a.MenuParentID, a.MenuLevel, a.MenuParentList, a.MenuSonList, a.Flag, ISNULL(b.MenuName, '根菜单') AS PName ");
            strSql.Append(" FROM          dbo.T_Menu AS a LEFT OUTER JOIN ");
            strSql.Append(" dbo.T_Menu AS b ON a.MenuParentID = b.id ");
            strSql.Append(" WHERE      (a.Flag = 1) ");
            strSql.Append(" UNION ALL ");
            strSql.Append(" SELECT     0 AS Expr1, '根课程' AS Expr2, '根课程' AS Expr3, - 1 AS Expr4, '0' AS Expr5, '0' AS Expr6, '0' AS Expr7, 1, '无' AS Expr8 ");
            strSql.Append(" ) AS a_1 ");
            strSql.Append(" ORDER BY MenuParentList ");



            var list = strSql.ToString().Exp_GetList(YXSQL.SQLConType.C1,
               DR => new Model.T_MenuExp()
               {
                   Flag = DR["Flag"].Exp_BoolTryParse(),
                   id = DR["id"].Exp_IntTryParse(),
                   MenuLevel = DR["MenuLevel"].Exp_IntTryParse(),
                   MenuName = DR["MenuName"].Exp_Trim(),
                   MenuParentID = DR["MenuParentID"].Exp_IntTryParse(),
                   MenuParentList = DR["MenuParentList"].Exp_Trim(),
                   MenuSonList = DR["MenuSonList"].Exp_Trim(),
                   MenuURL = DR["MenuURL"].Exp_Trim(),
                   PID = DR["MenuParentID"].Exp_IntTryParse(),
                   PName = DR["PName"].Exp_Trim(),
               }
               );
            return list;
        }
        public int GetMenuListPage(string PName, string Name)
        {
            var strSql = ExpandMSSQL.GetPageSQL(
                    "dbo.T_Menu AS a left JOIN  dbo.T_Menu AS b ON a.MenuParentID = b.id",
                    " a.flag=1 and a.MenuName like '%'+@Name+'%' and isnull(b.menuName,'根目录') like '%'+@PName+'%' "
                );
            var refInt = strSql.Exp_GetSingle(YXSQL.SQLConType.C1,
                 new SqlParameter("@Name", SqlDbType.NVarChar, 50) { Value = Name },
                new SqlParameter("@PName", SqlDbType.NVarChar, 50) { Value = PName });

            return refInt.Exp_IntTryParse();
        }

        public int GetSonCount(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  count(id) as id from T_Menu ");
            strSql.Append(" where MenuParentID=@id and flag=1 ");
            return strSql.ToString().Exp_GetSingle(YXSQL.SQLConType.C1, new SqlParameter("@id", SqlDbType.Int) { Value = id }).Exp_IntTryParse();
        }
        public int GetButtonCount(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  count(id) as id from T_Button ");
            strSql.Append(" where MenuID=@id and flag=1 ");
            return strSql.ToString().Exp_GetSingle(YXSQL.SQLConType.C1, new SqlParameter("@id", SqlDbType.Int) { Value = id }).Exp_IntTryParse();

        }

        public List<Model.T_Menu> GetListAllP()
        {

            string strSql = " SELECT     id, MenuName, MenuURL, MenuParentID, MenuLevel, MenuParentList, MenuSonList, Flag " +
                            " FROM         dbo.T_Menu  where flag=1 and menuparentid=0";
            var list = strSql.Exp_GetList(YXSQL.SQLConType.C1,
               DR => new Model.T_Menu()
               {
                   Flag = DR["Flag"].Exp_BoolTryParse(),
                   id = DR["id"].Exp_IntTryParse(),
                   MenuLevel = DR["MenuLevel"].Exp_IntTryParse(),
                   MenuName = DR["MenuName"].Exp_Trim(),
                   MenuParentID = DR["MenuParentID"].Exp_IntTryParse(),
                   MenuParentList = DR["MenuParentList"].Exp_Trim(),
                   MenuSonList = DR["MenuSonList"].Exp_Trim(),
                   MenuURL = DR["MenuURL"].Exp_Trim(),
               }
               );
            return list;

        }
        public List<Model.T_Menu> GetListSon(int id)
        {

            string strSql = " SELECT     id, MenuName, MenuURL, MenuParentID, MenuLevel, MenuParentList, MenuSonList, Flag " +
                            " FROM         dbo.T_Menu  where flag=1 and menuparentid=@id ";
            var list = strSql.Exp_GetList(YXSQL.SQLConType.C1,
               DR => new Model.T_Menu()
               {
                   Flag = DR["Flag"].Exp_BoolTryParse(),
                   id = DR["id"].Exp_IntTryParse(),
                   MenuLevel = DR["MenuLevel"].Exp_IntTryParse(),
                   MenuName = DR["MenuName"].Exp_Trim(),
                   MenuParentID = DR["MenuParentID"].Exp_IntTryParse(),
                   MenuParentList = DR["MenuParentList"].Exp_Trim(),
                   MenuSonList = DR["MenuSonList"].Exp_Trim(),
                   MenuURL = DR["MenuURL"].Exp_Trim(),
               },
               new SqlParameter("@id", SqlDbType.Int) { Value = id }
               );
            return list;

        }


        public List<Model.T_MenuExp> GetMenuListPage(int Begin, int Pager, string PName, string Name)
        {
            //            SELECT     TOP (100) PERCENT a.id, a.MenuName, a.MenuURL, a.MenuParentID, a.MenuLevel, a.MenuParentList, a.MenuSonList, a.Flag, b.MenuName AS PName
            //FROM         dbo.T_Menu AS a INNER JOIN
            //                      dbo.T_Menu AS b ON a.MenuParentID = b.id
            //WHERE     (a.Flag = 1)
            ////ORDER BY a.MenuParentList

            var strSql = ExpandMSSQL.GetPageSQL(
                        Begin,
                        Pager,
                        "a.id, a.MenuName, a.MenuURL, a.MenuParentID, a.MenuLevel, a.MenuParentList, a.MenuSonList, a.Flag,isnull( b.MenuName,'根目录') AS PName",
                        "dbo.T_Menu AS a left JOIN  dbo.T_Menu AS b ON a.MenuParentID = b.id",
                        " a.id ",
                        " a.flag=1 and a.MenuName like '%'+@Name+'%' and isnull(b.menuName,'根目录') like '%'+@PName+'%' ",
                        " a.MenuParentList ");

            var list = strSql.Exp_GetList(YXSQL.SQLConType.C1,
                DR => new Model.T_MenuExp()
                {
                    Flag = DR["Flag"].Exp_BoolTryParse(),
                    id = DR["id"].Exp_IntTryParse(),
                    MenuLevel = DR["MenuLevel"].Exp_IntTryParse(),
                    MenuName = DR["MenuName"].Exp_Trim(),
                    MenuParentID = DR["MenuParentID"].Exp_IntTryParse(),
                    MenuParentList = DR["MenuParentList"].Exp_Trim(),
                    MenuSonList = DR["MenuSonList"].Exp_Trim(),
                    MenuURL = DR["MenuURL"].Exp_Trim(),
                    PID = DR["MenuParentID"].Exp_IntTryParse(),
                    PName = DR["PName"].Exp_Trim(),
                },
                new SqlParameter("@Name", SqlDbType.NVarChar, 50) { Value = Name },
                new SqlParameter("@PName", SqlDbType.NVarChar, 50) { Value = PName }
                );


            return list;

        }
        public int DelExp(List<int> list)
        {
            return " T_Menu ".Exp_UpdateFlag(list, YXSQL.SQLConType.C1);
        }

    }
}
